var app = getApp(),
$ = require("../../utils/httpHelper.js")

Page({
  data: {
    page:1,
    pagesize:1,
    total_page:0,
    now_num:1,
    learnarr:[],
    isdialog:false,
    class_id:"",
    channel_id:'',
    urlsimg: [],
    isimg: false,
    width: 0,
    height: 0,
    imgheight: 0,
    imgwidth: 0
  },
  onLoad: function (options) {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height: res.windowHeight,
          width: res.windowWidth })
      }
    })
    if(options.class_id){
      this.setData({
        class_id: options.class_id,
        channel_id:options.channel_id
      })
    }
    this.playA()
    
  },
  onHide() {
    console.log("lister .. hide..")
    this.innerAudioContext.destroy()
  },
  onUnload(){
    console.log("lister .. unload..")
    this.innerAudioContext.destroy()
  },
  playA: function () {
    let par = {
      rd_session: wx.getStorageSync('rd_session'),
      page: this.data.page,
      page_size: this.data.pagesize,
      class_id:this.data.class_id,
      channel_id:this.data.channel_id
    }
    this.innerAudioContext = wx.createInnerAudioContext()
    if (this.data.learnarr.length >= this.data.now_num){
      this.innerAudioContext.src = this.data.learnarr[this.data.now_num-1].url;
      this.innerAudioContext.play()
      console.log("play....")
      console.log(this.data.learnarr[this.data.now_num - 1].index)
    }else{
      $.httpPost("learn", par, (data, err) => {
        let arr = this.data.learnarr
        arr.push(data.data.data)
        this.setData({
          learnarr: arr,
          total_page: data.data.total_page
        })
        this.innerAudioContext.autoplay = true
        this.innerAudioContext.src = data.data.data.url
      })
    }
    this.innerAudioContext.onEnded(() => {
      console.log("end...." + this.data.now_num)
      this.data.now_num++
      this.data.page++
      if (this.data.learnarr.length >= this.data.now_num) {
        this.innerAudioContext.src = this.data.learnarr[this.data.now_num - 1].url;
        this.innerAudioContext.play()
        console.log(this.data.learnarr[this.data.now_num - 1].index)
      }
      if (this.data.now_num <= this.data.total_page) {
        this.playA();
      }else{
        this.setData({
          isdialog:true
        })
      }
    })
  },
  audioPlay: function (e) {
    this.innerAudioContext.stop()
    let srcs = e.currentTarget.dataset.src
    let index = e.currentTarget.dataset.index
    console.log(index)
    this.setData({
      now_num: index,
      page: index
    })
    this.innerAudioContext.src = srcs
    this.innerAudioContext.play()
    // this.innerAudioContext.onEnded(() => {
    //   console.log('gggg2')
    //   if (this.data.now_num <= this.data.total_page) {
    //     this.playA();
    //   }
    // })
  },
  
  cancel(){
    this.setData({
      isdialog:false
    })
  },

  imageLoad: function (e) {
    console.log('jfjfjkdsjflkds')
    var _this = this;
    var $width = e.detail.width,    //获取图片真实宽度  
      $height = e.detail.height,
      ratio = $width / $height;   //图片的真实宽高比例  
    var viewWidth = this.data.width + 20,           //设置图片显示宽度，  
      viewHeight = this.data.width / ratio;    //计算的高度值     
    this.setData({
      imgwidth: viewWidth,
      imgheight: viewHeight
    })
    console.log(this.data.imgheight)
  },
  showimg(e) {
    let srcs = e.currentTarget.dataset.src
    let arr = []
    for (let src of srcs) {
      arr.push(src.url)
    }
    console.log(arr)
    this.setData({
      isimg: true,
      urlsimg: arr
    })
    // wx.previewImage({
    //   urls: arr
    // })

  },
  closeimg() {
    this.setData({
      isimg: false,
    })
  },
 
  totalking(){
    wx.navigateTo({
      url: '/pages/class/talking?class_id='+this.data.class_id
    })
  }
})
